<?php

namespace app\admin\controller;

use app\admin\model\Comment as ModelComment;
use app\common\LogService;
use think\facade\Log;

class Comment extends Base
{
    public function index()
    {
        return view();
    }
    public function forbidWord()
    {
        $words = getConfig('api', 'forbiddenWord')['words'];
        if (request()->isPost()) {
            $recordLog = function ($status, $content = []) {
                LogService::record(
                    name: '评论违禁词修改',
                    user_id: $this->uid,
                    status: $status,
                    content: $content,
                    user_type: $this->role

                );
            };
            $param = input();
            $path  = getAppConfPath('api') . 'forbiddenWord.php';
            $content = "<?php return ['words' => '" . $param['words'] . "'];";
            if (file_put_contents($path, $content)) {
                $recordLog(1, ['beforeData' => $words]);
                return success();
            } else {
                $recordLog(0, ['beforeData' => $words]);
                return error();
            }
        }
        return view('forbidden', ['words' => $words]);
    }
    public function list()
    {
        $list = ModelComment::list();
        return success($list);
    }
    public function del($id)
    {
        $recordLog = function ($status, $message = '') use ($id) {
            LogService::record(
                name: '批量删除评论',
                target_id: $id,
                user_id: $this->uid,
                status: $status,
                content: $message == '' ? [] : ['reason' => $message],
                user_type: 2

            );
        };
        $ids = explode(',', $id);
        $res = ModelComment::destroy($ids);
        if ($res) {
            $recordLog(1);
            return success();
        }
        $recordLog(0);
        return error();
    }
}
